<template>
  <el-dialog
    :title="!dataForm.id ? '新增岗位' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      validate-on-rule-change
      label-width="80px"
    >
      <el-form-item v-if="postType==0" label="部门名称" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="部门名称"></el-input>
      </el-form-item>

      <el-form-item v-if="postType==1" label="角色名称" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="角色名称"></el-input>
      </el-form-item>

      <el-form-item label="上级部门" prop="parentName">
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-popover ref="menuListPopover" placement="bottom-start" trigger="click">
      <el-tree
        :data="menuList"
        :props="menuListTreeProps"
        ref="menuListTree"
        expand-on-click-node
        @current-change="menuListTreeCurrentChangeHandle"
        :highlight-current="true"
      ></el-tree>
    </el-popover>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";

import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      postType: 1,
      roleList: [],
      menuList: [],
      menuListTreeProps: {
        children: "children",
        label: "lable",
        disabled: val => val.type == 1
      },

      dataForm: {},
      dataRule: {
        userName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        parentName: [
          { required: true, message: "上级部门不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      if (data.type == 1) {
        return false;
      }
      this.dataForm.parentId = data.value;
      this.dataForm.parentName = data.lable;
    },
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },
    init(id, postType, parent) {
      this.dataForm = {
        id: "",
        userName: "",
        password: "",
        comfirmPassword: "",
        salt: "",
        email: "",
        mobile: "",
        roleIdList: [],
        status: 1,
        parentName: "",
        parentId: ""
      };
      // this.dataForm.parentName = parent.lable;
      // this.dataForm.parentId = parent.value;
      this.postType = postType;
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/org/list"),
        method: "POST",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.menuList = data.list;
        this.visible = true;
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/org/${!this.dataForm.id ? "insert" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              userId: this.dataForm.id || undefined,
              value: this.dataForm.parentId,
              lable: this.dataForm.userName,
              type: this.postType
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
